#include "MainWindow.h"
#include "./ui_MainWindow.h"
#include "widgets/BlockStoreStateWidget.h"
#include <QDir>
#include <QMessageBox>
#include <QDebug>
#include "thirdparty/QXlsx/QXlsx/header/xlsxdocument.h"
#include "thirdparty/QXlsx/QXlsx/header/xlsxcell.h"
#include "widgets/AddMouldWidget.h"
#include "widgets/AddBlockWidget.h"
#include "widgets/AddConstructWidget.h"
#include "widgets/ModifyConstructWidget.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    
    // 初始化数据库（使用默认路径：当前程序目录下的data文件夹）
    DatabaseManager& dbManager = DatabaseManager::getInstance();
    if (dbManager.initialize()) {
        qDebug() << "数据库初始化成功";
    } else {
        qDebug() << "数据库初始化失败:" << dbManager.getLastError();
    }
    setupUi();
    ui->test_read_btn->hide();
    ui->test_write_btn->hide();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setupUi()
{
    {
        mAddMouldWidget = new AddMouldWidget();
        ui->tabWidget->addTab(mAddMouldWidget, QString::fromLocal8Bit("新增模具"));
        connect(ui->tabWidget, &QTabWidget::currentChanged, this, [this](int index){
            if (ui->tabWidget->currentWidget() == mAddMouldWidget) {
                mAddMouldWidget->setFocus();
            }
        });
        mAddMouldWidget->setFocus();
    }

    {
        mAddBlockWidget = new AddBlockWidget();
        ui->tabWidget->addTab(mAddBlockWidget, QString::fromLocal8Bit("新增活字块"));
        connect(ui->tabWidget, &QTabWidget::currentChanged, this, [this](int index){
            if (ui->tabWidget->currentWidget() == mAddBlockWidget) {
                mAddBlockWidget->init();
            }
        });
    }

    {
        mAddConstructWidget = new AddConstructWidget();
        ui->tabWidget->addTab(mAddConstructWidget, QString::fromLocal8Bit("新增施工表"));
        connect(ui->tabWidget, &QTabWidget::currentChanged, this, [this](int index){
            if (ui->tabWidget->currentWidget() == mAddConstructWidget) {
                mAddConstructWidget->init();
            }
        });
    }

    {
        mModifyConstructWidget = new ModifyConstructWidget();
        ui->tabWidget->addTab(mModifyConstructWidget, QString::fromLocal8Bit("修改施工表"));
        connect(ui->tabWidget, &QTabWidget::currentChanged, this, [this](int index){
            if (ui->tabWidget->currentWidget() == mModifyConstructWidget) {
                mModifyConstructWidget->init();
            }
        });
    }
}

void MainWindow::on_test_write_btn_clicked()
{
    // const QString fileName = QDir::currentPath() + "/demo.xlsx";
    // qDebug() << "Output file:" << fileName;
    // QXlsx::Document xlsx;
    // xlsx.write("A1", "Name");
    // xlsx.write("B1", "Score");
    // xlsx.write("C1", "Passed");

    // // 数据
    // xlsx.write(2, 1, "Alice");
    // xlsx.write(2, 2, 95);
    // xlsx.write(2, 3, true);

    // xlsx.write(3, 1, "Bob");
    // xlsx.write(3, 2, 83);
    // xlsx.write(3, 3, true);

    // xlsx.write(4, 1, "Cindy");
    // xlsx.write(4, 2, 58);
    // xlsx.write(4, 3, false);
    // if (!xlsx.saveAs(fileName)) {
    //     qCritical() << "Failed to save xlsx!";
    //     return;
    // }
    // qDebug() << "File saved.";
}


void MainWindow::on_test_read_btn_clicked()
{
    // const QString fileName = QDir::currentPath() + "/demo.xlsx";
    // QXlsx::Document xlsx(fileName);
    // if (!xlsx.load()) {
    //     // 对于当前版本，构造函数会尝试打开；有的版本没有显式 load 也能用
    //     qCritical() << "Failed to open xlsx!";
    // }

    // // 获取数据区域边界（第一行/列到最后一行/列）
    // QXlsx::CellRange range = xlsx.dimension();
    // int r1 = range.firstRow();
    // int c1 = range.firstColumn();
    // int r2 = range.lastRow();
    // int c2 = range.lastColumn();

    // qDebug() << "Dimension:" << r1 << c1 << "to" << r2 << c2;

    // for (int r = r1; r <= r2; ++r) {
    //     QStringList cols;
    //     for (int c = c1; c <= c2; ++c) {
    //         QVariant v = xlsx.read(r, c);        // 直接取值
    //         cols << (v.isNull() ? "" : v.toString());
    //     }
    //     qDebug().noquote() << cols.join('\t');    // 打印一行（以\t 分隔）
    // }
}

